package ckw.gui;

import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import ckw.database.model.Member;
import core.database.SexType;

public class WzdPgAddEditMember extends ModelValidationWizardPage {

	private boolean addMember;
	private Member member;

	private String editIdentificationField = "";

	private Text txtName;
	private Text txtSurname;
	private Combo cmbSex;
	private ComboViewer cmbVwrSex;
	private Text txtAddress;
	private Text txtIdentificationField;

	/**
	 * Create the wizard.
	 */
	public WzdPgAddEditMember(boolean addMember, Member member) {
		super("WzdPgAddEditMember");

		if (addMember) {
			setTitle("Add Member");
			setDescription("Add new Member to the Database.");
		} else {
			setTitle("Edit Member");
			setDescription("Edit an existing Member of the Database.");
		}

		this.addMember = addMember;
		this.member = member;
	}

	/**
	 * Create contents of the wizard.
	 * 
	 * @param parent
	 */
	public void createControl(Composite parent) {
		Composite container = new Composite(parent, SWT.NULL);

		setControl(container);
		container.setLayout(new GridLayout(2, false));

		new Label(container, SWT.NONE).setText("Name:");

		txtName = new Text(container, SWT.BORDER | SWT.SINGLE);
		txtName.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		new Label(container, SWT.NONE).setText("Surname:");

		txtSurname = new Text(container, SWT.BORDER | SWT.SINGLE);
		txtSurname.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		new Label(container, SWT.NONE).setText("Sex:");

		cmbVwrSex = new ComboViewer(container, SWT.READ_ONLY);
		cmbSex = cmbVwrSex.getCombo();
		cmbSex.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		setSexInput();

		new Label(container, SWT.NONE).setText("Address:");

		txtAddress = new Text(container, SWT.BORDER | SWT.SINGLE);
		txtAddress.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		new Label(container, SWT.NONE).setText("Identification field:");

		txtIdentificationField = new Text(container, SWT.BORDER | SWT.SINGLE);
		txtIdentificationField.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

		initValues();
		setVerifyListeners();
	}

	private void initValues() {
		if (addMember) {
			setPageComplete(false);
		} else {
			txtName.setText(member.getName());
			txtSurname.setText(member.getSurname());
			SexType sexType = member.getSex();
			if (sexType != null) {
				cmbVwrSex.setSelection(new StructuredSelection(sexType));
			}
			txtAddress.setText(member.getAddress());
			txtIdentificationField.setText(member.getIdentificationField());

			editIdentificationField = member.getIdentificationField();
		}
	}

	private void setVerifyListeners() {
		txtName.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
		txtSurname.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});

		cmbVwrSex.addSelectionChangedListener(new ISelectionChangedListener() {
			@Override
			public void selectionChanged(SelectionChangedEvent arg0) {
				validate();
			}
		});

		txtAddress.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
		txtIdentificationField.addModifyListener(new ModifyListener() {
			@Override
			public void modifyText(ModifyEvent arg0) {
				validate();
			}
		});
	}

	private void setSexInput() {
		cmbVwrSex.setContentProvider(new ArrayContentProvider());
		SexType[] sexTypeArray = { SexType.MALE, SexType.FEMALE };
		cmbVwrSex.setInput(sexTypeArray);
	}

	@Override
	protected void validate() {
		String lastIdentificationFieldValue = "";
		boolean isIdentificationFieldNotValid = false;

		String name = txtName.getText().trim();
		String surname = txtSurname.getText().trim();
		String address = txtAddress.getText().trim();
		String identificationField = txtIdentificationField.getText().trim();

		if (MainWindow.isInDebugMode()) {
			clearErrorState();
			return;
		}

		// error
		if (name.length() == 0) {
			setErrorState("Name cannot be empty.");
			return;
		}
		if (surname.length() == 0) {
			setErrorState("Surname cannot be empty.");
			return;
		}
		if (((StructuredSelection) cmbVwrSex.getSelection()).isEmpty()) {
			setErrorState("Sex must be selected.");
			return;
		}
		if (address.length() == 0) {
			setErrorState("Address cannot be empty.");
			return;
		}
		if (identificationField.length() == 0) {
			setErrorState("Identification field cannot be empty.");
			return;
		}
		if (addMember) {
			if (lastIdentificationFieldValue.compareTo(identificationField) != 0) {
				lastIdentificationFieldValue = identificationField;
				isIdentificationFieldNotValid = manager.isMemberIdentificationFieldTaken(identificationField);
			}
		} else {
			if (identificationField.compareTo(editIdentificationField) != 0 && lastIdentificationFieldValue.compareTo(identificationField) != 0) {
				lastIdentificationFieldValue = identificationField;
				isIdentificationFieldNotValid = manager.isMemberIdentificationFieldTaken(identificationField);
			}
		}
		if (isIdentificationFieldNotValid) {
			setErrorState("Identification field already exists.");
			return;
		}

		// ok
		clearErrorState();
	}

	@Override
	public void update() {
		member.setName(txtName.getText().trim());
		member.setSurname(txtSurname.getText().trim());
		IStructuredSelection structuredSelection = (IStructuredSelection) cmbVwrSex.getSelection();
		member.setSex((SexType) structuredSelection.getFirstElement());
		member.setAddress(txtAddress.getText().trim());
		member.setIdentificationField(txtIdentificationField.getText().trim());
	}
}
